package com.scrumtrek.trainings.tdd;

/**
 * Created with IntelliJ IDEA.
 * User: skashin
 * Date: 20.02.13
 * Time: 18:48
 * To change this template use File | Settings | File Templates.
 */
public class Book extends Item {
    private Author author;
    private String year;
    private String publisher;

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthorName(String authorName) {
        Author findedAuthor = Db.getInstance().findAuthorByName(authorName);

        if(findedAuthor==null){
            findedAuthor = new Author();
            findedAuthor.setName(authorName);
            Db.getInstance().getAuthors().add(findedAuthor);
        }
        this.author = findedAuthor;
    }

    public String getFormattedTitle(){
        String formattedTitle = new String();
        formattedTitle += getTitle();
        if (getPublisher() != null) {
            formattedTitle += " ["+ getPublisher()+"]";
        }
        if (getYear() != null) {
            formattedTitle += " ["+ getYear()+"]";
        }
        return formattedTitle;
    }
}
